﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ueEngine.Biz.Inf.Entities.DBSupport;
using ueEngine.Framework.Exception;

namespace ueEngine.Biz.Inf.LogicEntity.SupportData
{
    public abstract class IAccessible<dbEntity, lcEntity>
        where dbEntity : BaseEntity<dbEntity>,new()
        where lcEntity : class
    {
        protected abstract void Covariant(dbEntity dbModel);
        protected abstract dbEntity Inverter(lcEntity model);
        public lcEntity Set(dbEntity dbModel)
        {
            Covariant(dbModel);
            return this as lcEntity;
        }
        public lcEntity Element(Expression<Func<dbEntity, bool>> where)
        {
            var item = new dbEntity().Get(where);
            if (item != null)
            {
                Covariant(item);
                return this as lcEntity;
            }
            return null;
        }
        public List<lcEntity> Elements(Expression<Func<dbEntity, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var list = ctx.Set<dbEntity>().Where(where)
                    .ToList();// ctx.a.Where(where).Select(select).ToList();
                return list.Select(c => this.Set(c)).ToList();
            }
        }

        public lcEntity PostAdd()
        {
            var item = Inverter(this as lcEntity);
            if (item.PostAdd() > 0)
            {
                Covariant(item);
                return this as lcEntity;
            }
            return null;
            //using (Context ctx = new Context())
            //{
            //    var item = Inverter(this as lcEntity);
            //    ctx.Entry(item).State = System.Data.EntityState.Added;
            //    if (ctx.SaveChanges() > 0)
            //    {
            //        Covariant(item);
            //        return this as lcEntity;
            //    }
            //    return null;
            //}
        }
        public lcEntity PostModify()
        {
            var item = Inverter(this as lcEntity);
            if (item.PostModify() > 0)
            {
                Covariant(item);
                return this as lcEntity;
            }
            return null;
            //using (Context ctx = new Context())
            //{
            //    var item = Inverter(this as lcEntity);
            //    ctx.Entry(item).State = System.Data.EntityState.Modified;
            //    if (ctx.SaveChanges() > 0)
            //    {
            //        Covariant(item);
            //        return this as lcEntity;
            //    }
            //    return null;
            //}
        }
        public lcEntity PostDelete()
        {
            var item = Inverter(this as lcEntity);
            if (item.PostDelete() > 0)
            {
                Covariant(item);
                return this as lcEntity;
            }
            return null;
            //using (Context ctx = new Context())
            //{
            //    var item = Inverter(this as lcEntity);
            //    ctx.Entry(item).State = System.Data.EntityState.Deleted;
            //    if (ctx.SaveChanges() > 0)
            //    {
            //        Covariant(item);
            //        return this as lcEntity;
            //    }
            //    return null;
            //}
        }
    }
}
